<?php
/**
 * Created by PhpStorm.
 * User: ysongyang
 * Date: 2016/6/7
 * Time: 12:19
 */
namespace Admin\Controller;

class ConfigController extends CommonController{

    function _initialize()
    {
        parent::_initialize();
        $this->_config = D('config');
    }
    // 文章首页
    public function index()
    {
        $data = $this->_config->order('conf_order','asc')->select();
        foreach($data as $key=>$val){
            switch($val['conf_field_type']){
                case 'input':
                    $data[$key]['_html'] = "<input type='text' class='lg' name='".$val['conf_name']."' value='".$val['conf_content']."' />";
                    break;
                case 'textarea':
                    $data[$key]['_html'] = "<textarea class='lg' name='".$val['conf_name']."'>".$val['conf_content']."</textarea>";
                    break;
                case 'radio':
                    //1|开启,0|关闭
                    $str = explode(',',$val['conf_field_value']);
                    $arr = '';
                    foreach($str as $k=>$v){
                        $r = explode('|',$v);
                        $checked =$val['conf_content']==$v[0]?'checked':'';
                        $arr .="<input type='radio' name='".$val['conf_name']."' value='".$r[0]."' ".$checked.">".$r[1]."&nbsp;&nbsp;";
                    }

                    $data[$key]['_html'] = $arr;
                    break;
            }
        }
        $this->assign('data',$data);
        $this->display();
    }

    //添加及添加表单处理
    public function add()
    {
        if(IS_POST){
            $conf_name = $_POST['conf_name'];
            if($this->_config->where(array('conf_name'=>$conf_name))->find()){
                $this->error('配置名称已存在，请重新添加！');
            }else{
                $res = $this->_config ->add($_POST);
                if($res){
                    $this->success('添加配置成功！', U(MODULE_NAME . '/Config/index'));
                }else{
                    $this->error('添加配置失败！');
                }
            }
        }else{
            $this->display();
        }
    }

    public function edit(){
        if(IS_POST){
            $conf_Id = $_GET['conf_id'];
            $res = $this->_config ->where(array('conf_id'=>$conf_Id))->save($_POST);
                if($res){
                    $this->putFile();
                    $this->success('修改配置成功！', U(MODULE_NAME . '/Config/index'));
                }else{
                    $this->error('修改配置失败！');
                }
        }else{
            $conf_Id = I('conf_id');
            $this->conf = $this->_config ->where(array('conf_id'=>$conf_Id))->find();
            $this->display();
        }
    }
    
    //异步删除
    public function delete()
    {
        $conf_id = I('conf_id');
        $res = $this->_config->where(array('conf_id'=>$conf_id))->delete();
        if($res){
            $this->putFile();
            $data = ['status'=>0,'info'=>'删除配置成功！'];
        }else{
            $data = ['status'=>1,'info'=>'删除配置失败，请稍后再试！',];
        }
        $this->ajaxReturn($data);

    }

    //异步更新排序
    public function change(){
        $conf_id = $_POST['conf_id'];
        $conf_order = $_POST['conf_order'];
        $res = $this->_config->where(array('conf_id'=>$conf_id))->save(array('conf_order'=>$conf_order));
        if($res){
            $data = ['status'=>0,'info'=>'排序更新成功！'];
        }else{
            $data = ['status'=>1,'info'=>'排序更新失败，请稍后再试！',];
        }
        $this->ajaxReturn($data);
    }


    //配置文件提交
    public function report(){
        if(IS_POST){
            unset($_POST['ord']);
            foreach ($_POST as $key => $value)
            {
                $this->_config -> where(array('conf_name'=>$key))->save(array('conf_content'=>$value));
            }
            $this->putFile();
            $this->success('配置项更新成功！', U(MODULE_NAME . '/Config/index'));
        }

    }

    //网站配置写入文件
    public function putFile(){
        $config = $this->_config->field(array('conf_name','conf_content'))->select();
        foreach($config as $key=>$val){
           $str[$val['conf_name']] = $val['conf_content'];
            //$str[]=".$val['conf_name']."."=>".$val['conf_content'];
        }
        $path = APP_PATH.'/Common/Conf/web.php';  //定义网站配置文件存放路径
        $arr = "<?php return ".var_export($str,true).";";
        file_put_contents($path,$arr);
    }
}